package io.plus.server.command.command_handler;

import io.plus.common.io;
import io.plus.common.packet.Group;
import io.plus.common.packet.User;
import io.plus.common.packet.message.UserRequestMessage;
import io.plus.common.packet.metadata.Command;
import io.plus.common.packet.metadata.ResponseMessage;
import io.plus.common.protocol.Packet;
import io.plus.engine.pulse.AsynchronousSocketChannelContext;
import io.plus.server.Status;
import io.plus.server.UserConfiguration;
import io.plus.server.command.CommandHandler;
import io.plus.server.store.Store;
import io.plus.server.utils.ImKit;
import io.plus.server.utils.JsonKit;
import tool.plus.$;

import java.util.ArrayList;
import java.util.List;


/**
 * 获取用户信息消息命令
 */
public class UserCommandHandler implements CommandHandler {

    /**
     * 处理在线用户好友及群组用户;
     *
     * @param groups
     * @param flag(0：好友,1:群组)
     * @return
     */
    private static List<Group> initOnlineUserFriendsGroups(List<Group> groups, Integer flag) {
        if (groups == null || groups.isEmpty()) {
            return null;
        }
        //处理好友分组在线用户相关信息;
        List<Group> onlineGroups = new ArrayList<Group>();
        for (Group group : groups) {
            Group copyGroup = group;
            List<User> users = null;
            if (flag == 1) {
                users = io.getAllUserByGroup(group.getId());
            } else if (flag == 0) {
                users = group.getUserList();
            }
            if (users != null && !users.isEmpty()) {
                List<User> copyUsers = new ArrayList<User>();
                for (User userObj : users) {
                    User onlineUser = io.getUser(userObj.getId());
                    copyUsers.add(onlineUser);
                }
                copyGroup.setUserList(copyUsers);
            }
            onlineGroups.add(copyGroup);
        }
        return onlineGroups;
    }

    @Override
    public Command command() {
        return Command.get_user_info_request;
    }

    /**
     * 根据用户id获取用户在线及离线用户;
     */
    public User getUserInfo(UserConfiguration userConfiguration, String userid) {
        User user = null;

        //消息持久化助手;
        Store store = userConfiguration.getStore();
        boolean isStore = true;
        if (isStore) {
            user = store.getUser(userid);
            if (user == null) {
                return null;
            }
            //user.setFriends(store.getAllFriendUsers(userid, type));
            user.setGroups(store.getGroupList(userid));
            return user;
        } else {
            user = io.getUser(userid);
            if (user == null) {
                return null;
            }
            User copyUser = user;
            //在线用户;
            {
                //处理好友分组在线用户相关信息;
                List<Group> friends = user.getFriends();
                List<Group> onlineFriends = initOnlineUserFriendsGroups(friends, 0);
                if (onlineFriends != null) {
                    copyUser.setFriends(onlineFriends);
                }
                //处理群组在线用户相关信息;
                List<Group> groups = user.getGroups();
                List<Group> onlineGroups = initOnlineUserFriendsGroups(groups, 1);
                if (onlineGroups != null) {
                    copyUser.setGroups(onlineGroups);
                }
                return copyUser;
                //所有用户(在线+离线);
            }
        }
    }

    @Override
    public Packet handler(UserConfiguration userConfiguration, AsynchronousSocketChannelContext asynchronousSocketChannelContext, Packet packet) throws Exception {
        UserRequestMessage userReqBody = JsonKit.toBean(packet.getBody(), UserRequestMessage.class);
        User user = null;
        ResponseMessage resPacket = null;

        String userId = userReqBody.getUserId();
        if ($.isNullOrEmpty(userId)) {
            return ImKit.convertResponsepPacket(asynchronousSocketChannelContext, new ResponseMessage(Command.get_user_info_response, Status.C10004));
        }
        //(0:所有在线用户,1:所有离线用户,2:所有用户[在线+离线]);
        //Integer type = userReqBody.getType() == null ? 2 : userReqBody.getType();
        if (0 == userReqBody.getType() || 1 == userReqBody.getType() || 2 == userReqBody.getType()) {
            user = getUserInfo(userConfiguration, userId);
            resPacket = new ResponseMessage(Command.get_user_info_response, Status.C10005);
        }
        if (user == null) {
            return ImKit.convertResponsepPacket(asynchronousSocketChannelContext, new ResponseMessage(Command.get_user_info_response, Status.C10004));
        }
        resPacket.setData(user);
        return ImKit.convertResponsepPacket(asynchronousSocketChannelContext, resPacket);
    }
}
